package com.xiyun.aaa;

import java.util.Scanner;

public class ArrayCube {
    public static void resolve(String[] args) {
        ArrayQueue queue = new ArrayQueue(4);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(how):");
            System.out.println("e(xit):");
            System.out.println("a(dd):");
            System.out.println("g(et):");
            System.out.println("p(eak):");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    queue.showQueue();break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'a':
                    queue.addQueue(new Scanner(System.in).nextInt());break;
                case 'g':
                    try {
                        int get = queue.getQueue();
                        System.out.println("data is " + get);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 'p':
                    try {
                        int peak = queue.peak();
                        System.out.println("data is " + peak);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
        System.out.println("exit by handle");

    }
}
class ArrayQueue{
    private int maxSize;
    private int front;
    private int rear;
    private int[] data;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        data = new int[maxSize];
        front = 0;
        rear = 0;
    }

    public boolean isFull(){
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty(){
        return front == rear;
    }

    public void addQueue(int n){
        if (isFull()) {
            System.out.println("queue is full");
            return;
        }
        data[rear % maxSize] = n;
        rear = (rear + 1) % maxSize;
    }

    public int getQueue(){
        if (isEmpty()) {
            throw new RuntimeException("queue is Empty");
        }
        int datum = data[front];
        front = (front + 1) % maxSize;
        return datum;
    }

    public void showQueue(){
        if (isEmpty()) {
            System.out.println("queue is empty,nothing tu show");
            return;
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, data[i % maxSize]);
        }
    }

    public int peak(){
        if (isEmpty()) {
            throw new RuntimeException("noting to show");
        }
        return data[(front + 1) % maxSize];
    }

    private int size(){
        return (rear + maxSize - front) % maxSize;
    }
}
